/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.serli.sample.person.model.service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

/**
 *
 * @author serli
 */
public class PeopleServiceClientFactory {
    
    private final static Logger logger = Logger.getLogger(com.serli.sample.person.model.service.PeopleServiceClientFactory.class.getName());


    public PeopleServiceRemote getService(TypeService type) throws NamingException {
        
        PeopleServiceRemote peopleService = null;
        
        if (type == TypeService.SCA_EJB_Remote) {
            Properties p = new Properties();
            p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
            p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
            InitialContext ctx = new InitialContext(p);

            peopleService = (PeopleServiceRemote) ctx.lookup("people-model-ear-1.1/PeopleService-RMI/remote");
        }
        else if (type == TypeService.SOA_WS_SOAP) {

            URL url = null;
            try {
                url = new URL("http://127.0.0.1:8080/people-context-web/PeopleService");
            } catch (MalformedURLException e) {
                logger.warning("Failed to create URL for the wsdl Location: 'http://127.0.0.1:8080/people-context-web/PeopleService', retrying as a local file");
                logger.warning(e.getMessage());
            }
            
            Service wss = Service.create(url, new QName("http://ws.service.model.person.sample.serli.com/", "PeopleService"));
            peopleService = wss.getPort(new QName("http://ws.service.model.person.sample.serli.com/", "PeopleService-WSPort"), PeopleServiceRemote.class);
        }
        
        
        return peopleService;
    }
}
